home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / obsolete / cw_bselector.pro < prev    next >
Encoding:
Text File  |  1997-07-08  |  8.2 KB  |  244 lines

  1. ; $Id: cw_bselector.pro,v 1.8 1997/01/15 04:02:19 ali Exp $
  2. ;
  3. ; Copyright (c) 1993-1997, Research Systems, Inc.  All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;+
  6. ;
  7. ; NOTE:  This routine has been made obsolete because it has been replaced
  8. ;        by WIDGET_DROPLIST.
  9. ;
  10. ; NAME:
  11. ;    CW_BSELECTOR
  12. ;
  13. ; PURPOSE:
  14. ;    CW_BSELECTOR is a compound widget that appears as a pull-down
  15. ;    menu whose label shows the widget's current value. When the button
  16. ;    is pressed, the menu appears and the newly selected value becomes
  17. ;    the new title of the pull-down menu.
  18. ;
  19. ; CATEGORY:
  20. ;    Compound widgets.
  21. ;
  22. ; CALLING SEQUENCE:
  23. ;        widget = CW_BSELECTOR(Parent, Names)
  24. ;
  25. ;    To get or set the value of a CW_BSELECTOR, use the GET_VALUE and
  26. ;    SET_VALUE keywords to WIDGET_CONTROL. The value of a CW_BSELECTOR
  27. ;    is the index of the selected item.
  28. ;
  29. ; INPUTS:
  30. ;       Parent:        The ID of the parent widget.
  31. ;    Names:        A string array, containing one string per button,
  32. ;            giving the name of each button.
  33. ;
  34. ; KEYWORD PARAMETERS:
  35. ;    EVENT_FUNCT:    The name of an optional user-supplied event function 
  36. ;            for buttons. This function is called with the return
  37. ;            value structure whenever a button is pressed, and 
  38. ;            follows the conventions for user-written event
  39. ;            functions.
  40. ;    FONT:        The name of the font to be used for the button
  41. ;            titles. If this keyword is not specified, the default
  42. ;            font is used.
  43. ;    FRAME:        Specifies the width of the frame to be drawn around
  44. ;            the base.
  45. ;    IDS:        A named variable into which the button IDs will be
  46. ;            stored, as a longword vector.
  47. ;    LABEL_LEFT:    Creates a text label to the left of the buttons.
  48. ;    LABEL_TOP:    Creates a text label above the buttons.
  49. ;    MAP:        If set, the base will be mapped when the widget
  50. ;            is realized (the default).
  51. ;    RETURN_ID:    If set, the VALUE field of returned events will be
  52. ;            the widget ID of the button.
  53. ;    RETURN_INDEX:    If set, the VALUE field of returned events will be
  54. ;            the zero-based index of the button within the base.
  55. ;            THIS IS THE DEFAULT.
  56. ;    RETURN_NAME:    If set, the VALUE field of returned events will be
  57. ;            the name of the button within the base.
  58. ;    RETURN_UVALUE:    An array of user values to be associated with
  59. ;            each button. Selecting the button sets the uvalue
  60. ;            of the CW_BSELECTOR to the button's uvalue and
  61. ;            returns the uvalue in the value field of the event
  62. ;            structure.  If this keyword isn't specified, the
  63. ;            CW_BSELECTOR's uvalue remains unchanged.
  64. ;    SET_VALUE:    The initial value of the buttons. This keyword is 
  65. ;            set to the index of the Names array element desired.
  66. ;            So if it is desired that the initial value be the 
  67. ;            second element of the Names array, SET_VALUE would
  68. ;            be set equal to 1. This is equivalent to the later 
  69. ;            statement:
  70. ;
  71. ;            WIDGET_CONTROL, widget, set_value=value
  72. ;
  73. ;    UVALUE:        The user value to be associated with the widget.
  74. ;    XOFFSET:    The X offset of the widget relative to its parent.
  75. ;    YOFFSET:    The Y offset of the widget relative to its parent.
  76. ;
  77. ; OUTPUTS:
  78. ;       The ID of the created widget is returned.
  79. ;
  80. ; SIDE EFFECTS:
  81. ;    This widget generates event structures with the following definition:
  82. ;
  83. ;        event = { ID:0L, TOP:0L, HANDLER:0L, INDEX:0, VALUE:0 }
  84. ;
  85. ;    The INDEX field is the index (0 based) of the menu choice. VALUE is
  86. ;    either the INDEX, ID, NAME, or BUTTON_UVALUE of the button,
  87. ;    depending on how the widget was created.
  88. ;
  89. ; RESTRICTIONS:
  90. ;    Only buttons with textual names are handled by this widget.
  91. ;    Bitmaps are not understood.
  92. ;
  93. ; MODIFICATION HISTORY:
  94. ;    1 April 1993, DMS,  Adapted from CW_BGROUP.
  95. ;    22 Dec. 1993, KDB,  Corrected documentation for keyword SET_VALUE.
  96. ;-
  97.  
  98.  
  99. pro CW_BSELECTOR_SETV, id, value
  100.   ON_ERROR, 2                ;return to caller
  101.   stash = WIDGET_INFO(id, /CHILD)    ;Get state from 1st child
  102.   WIDGET_CONTROL, stash, GET_UVALUE=s, /NO_COPY
  103.   if value lt 0 or value ge n_elements(s.ids) then $
  104.     MESSAGE, 'Button value must be from 0 to n_buttons -1.', /INFO $
  105.   ELSE BEGIN  
  106.       WIDGET_CONTROL, s.ids(value), GET_VALUE=v    ;Get button label
  107.       WIDGET_CONTROL, s.menu, SET_VALUE=v    ;Set menu label
  108.       if s.uvret then WIDGET_CONTROL, id, SET_UVALUE=s.ret_arr(value)
  109.       s.select = value            ;save button that's selected
  110.   ENDELSE
  111.   WIDGET_CONTROL, stash, SET_UVALUE=s, /NO_COPY  
  112. end
  113.  
  114.  
  115.  
  116. function CW_BSELECTOR_GETV, id
  117.   ON_ERROR, 2                        ;return to caller
  118.   stash = WIDGET_INFO(id, /CHILD)    ;Get state from 1st child
  119.   WIDGET_CONTROL, stash, GET_UVALUE=s, /NO_COPY
  120.   ret = s.select
  121.   WIDGET_CONTROL, stash, SET_UVALUE=s, /NO_COPY  
  122.   return, ret
  123. end
  124.  
  125.  
  126.  
  127. function CW_BSELECTOR_EVENT, ev
  128.   base = ev.handler
  129.   stash = WIDGET_INFO(base, /CHILD)    ;Get state from 1st child
  130.   WIDGET_CONTROL, stash, GET_UVALUE=s, /NO_COPY
  131.   WIDGET_CONTROL, ev.id, get_uvalue=uvalue  ;The button index
  132.   s.select = uvalue            ;Save the selected index
  133.   rvalue = s.ret_arr(uvalue)
  134.   WIDGET_CONTROL, ev.id, GET_VALUE = v    ;Copy button's label to menu
  135.   WIDGET_CONTROL, s.menu, SET_VALUE = v
  136.   if s.uvret then WIDGET_CONTROL, base, SET_UVALUE=rvalue
  137.   efun = s.efun
  138.   WIDGET_CONTROL, stash, SET_UVALUE = s, /NO_COPY
  139.  
  140.   st = { ID:base, TOP:ev.top, HANDLER:0L, INDEX: uvalue, $  ;Return value
  141.         VALUE: rvalue }
  142.   if efun ne '' then return, CALL_FUNCTION(efun, st) $
  143.   else return, st
  144. end
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152. function CW_BSELECTOR, parent, names, EVENT_FUNCT = efun, $
  153.     RETURN_UVALUE = return_uvalue, $
  154.     FONT=font, FRAME=frame, IDS=ids, LABEL_TOP=label_top, $
  155.     LABEL_LEFT=label_left, MAP=map, RETURN_ID=return_id, $
  156.     RETURN_INDEX=return_index, RETURN_NAME=return_name, $
  157.     SET_VALUE=sval, UVALUE=uvalue, $ XOFFSET=xoffset, XSIZE=xsize, $
  158.     YOFFSET=yoffset, YSIZE=ysize
  159.  
  160.  
  161. ;  ON_ERROR, 2                        ;return to caller
  162.  
  163.   ; Set default values for the keywords
  164.   version = WIDGET_INFO(/version)
  165.   if (version.toolkit eq 'OLIT') then def_space_pad = 4 else def_space_pad = 3
  166.   IF (N_ELEMENTS(frame) eq 0)        then framet = 0 else framet = frame
  167.   IF (N_ELEMENTS(map) eq 0)        then map=1
  168.   IF (N_ELEMENTS(uvalue) eq 0)        then uvalue = 0
  169.   IF (N_ELEMENTS(xoffset) eq 0)        then xoffset=0
  170.   IF (N_ELEMENTS(xsize) eq 0)        then xsize = 0
  171.   IF (N_ELEMENTS(yoffset) eq 0)        then yoffset=0
  172.   IF (N_ELEMENTS(ysize) eq 0)        then ysize = 0
  173.  
  174.   top_base = 0L
  175.   next_base = parent
  176.   if (n_elements(label_top) ne 0) then begin
  177.     next_base = WIDGET_BASE(next_base, XOFFSET=xoffset, YOFFSET=yoffset, $
  178.         FRAME=framet, /COLUMN)
  179.     top_base = next_base
  180.     framet = 0                ;Only one frame
  181.     junk = WIDGET_LABEL(next_base, value=label_top)
  182.   endif else next_base = parent
  183.  
  184.   if (n_elements(label_left) ne 0) then begin
  185.     next_base = WIDGET_BASE(next_base, XOFFSET=xoffset, YOFFSET=yoffset, $
  186.           FRAME=framet, /ROW)
  187.     junk = WIDGET_LABEL(next_base, value=label_left)
  188.     framet = 0                ;Only one frame
  189.     if (top_base eq 0L) then top_base = next_base
  190.   endif
  191.  
  192.   ; We need some kind of outer base to hold the users UVALUE
  193.   if (top_base eq 0L) then begin
  194.     top_base = WIDGET_BASE(next_base, XOFFSET=xoffset, YOFFSET=yoffset, $
  195.             FRAME = framet)
  196.     next_base = top_base
  197.   endif
  198.  
  199.   ; Set top level base attributes
  200.   WIDGET_CONTROL, top_base, MAP=map, EVENT_FUNC='CW_BSELECTOR_EVENT', $
  201.      FUNC_GET_VALUE='CW_BSELECTOR_GETV', PRO_SET_VALUE='CW_BSELECTOR_SETV', $
  202.      SET_UVALUE=uvalue
  203.   if n_elements(sval) le 0 then sval = 0    ;Default selection index
  204.  
  205.   len = max(strlen(names), i)            ;Longest string = 1st value
  206.   len1 = strlen(names(sval))            ;Initial string length
  207.   if len gt len1 then $
  208.     i = names(sval) + string(replicate(32B, len-len1+2)) $  ;+ slop
  209.   else i = names(sval)
  210.   menu = WIDGET_BUTTON(next_base, /MENU, value = i)
  211.  
  212.   n = n_elements(names)
  213.   ids = lonarr(n)
  214.   for i = 0, n-1 do begin
  215.     if (n_elements(font) eq 0) then begin
  216.       ids(i) = WIDGET_BUTTON(menu, value=names(i), UVALUE=i)
  217.     endif else begin
  218.       ids(i) = WIDGET_BUTTON(menu, value=names(i), FONT=font, UVALUE=i)
  219.     endelse
  220.   endfor
  221.  
  222.   
  223.     ;Make returned value array
  224.   return_uvals = 0
  225.   if KEYWORD_SET(RETURN_ID) then ret_arr = ids $
  226.   else if KEYWORD_SET(RETURN_NAME) then ret_arr = names $
  227.   else if KEYWORD_SET(RETURN_UVALUE) then begin
  228.     ret_arr = return_uvalue
  229.     return_uvals = 1
  230.   endif else ret_arr = indgen(n)
  231.  
  232.   stash = WIDGET_INFO(top_base, /CHILD)    ;Affix state to 1st child
  233.   if n_elements(efun) le 0 then efun = ''
  234.  
  235.   WIDGET_CONTROL, stash, SET_UVALUE= { $
  236.     menu: menu, $
  237.     efun : efun, $            ; Name of event fcn
  238.         ret_arr: ret_arr, $        ; Vector of event values
  239.     select : sval, $
  240.     uvret : return_uvals, $
  241.     ids:ids }            ; Ids of buttons
  242.   return, top_base
  243. END
  244.